Public Class frmDivision

    Private Sub frmDivision_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadLeagues()
    End Sub
    Private Sub loadLeagues()
        'Get LeagueList

        Dim myLeagueList As coachingAppObjects.ro_leagueRootList
        myLeagueList = coachingAppObjects.ro_leagueRootList.GetLeagueRootList
        Me.cboLeagues.DataSource = myLeagueList
    End Sub
    Private Sub loadDivisions()
        Dim myLeague As coachingAppObjects.rw_leagueRoot
        myLeague = coachingAppObjects.rw_leagueRoot.GetleagueRoot(CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId)

        Dim item As coachingAppObjects.ro_divisionChild

        'Get DivisionList
        Dim myDivisionList As coachingAppObjects.ro_divisionRootList
        myDivisionList = coachingAppObjects.ro_divisionRootList.GetDivisionRootList(myLeague.LeagueId)
        'Me.cboExistingDivisions.DataSource = myDivisionList

        cboExistingDivisions.Items.Clear()
        cboExistingDivisions.Items.Add("Add New")
        For Each item In myDivisionList
            cboExistingDivisions.Items.Add(item)
        Next
        cboExistingDivisions.SelectedIndex = 0

    End Sub
    Private Sub cboLeagues_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLeagues.SelectedIndexChanged
        loadDivisions()
    End Sub
    Private Sub cboExistingDivisions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboExistingDivisions.SelectedIndexChanged
        If Not cboExistingDivisions.Text = "Add New" Then
            Dim myLeague As coachingAppObjects.rw_leagueRoot
            myLeague = coachingAppObjects.rw_leagueRoot.GetleagueRoot(CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId)

            Dim myDivision As coachingAppObjects.rw_divisionRoot
            myDivision = coachingAppObjects.rw_divisionRoot.GetDivisionRoot(CType(cboExistingDivisions.SelectedItem, coachingAppObjects.ro_divisionChild).DivisionId, myLeague.LeagueId)
            Me.txtDivision.Text = myDivision.DivisionName
            Me.txtDivisionDesc.Text = myDivision.DivisionDescription
        Else
            Me.txtDivision.Text = ""
            Me.txtDivisionDesc.Text = ""
        End If
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If cboExistingDivisions.Text = "Add New" Then
            Dim myDivision As coachingAppObjects.rw_divisionRoot
            myDivision = coachingAppObjects.rw_divisionRoot.NewDivisionRoot(0, 0)
            Dim myLeague As coachingAppObjects.rw_leagueRoot
            myLeague = coachingAppObjects.rw_leagueRoot.GetleagueRoot(CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId)

            myDivision.leagueId = myLeague.LeagueId
            myDivision.DivisionName = Me.txtDivision.Text
            myDivision.DivisionDescription = Me.txtDivisionDesc.Text
            myDivision.Save()
            loadDivisions()
        Else
            Dim mydivision As coachingAppObjects.rw_divisionRoot
            Dim myLeague As coachingAppObjects.rw_leagueRoot
            myLeague = coachingAppObjects.rw_leagueRoot.GetleagueRoot(CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId)
            mydivision = coachingAppObjects.rw_divisionRoot.GetDivisionRoot(CType(cboExistingDivisions.SelectedItem, coachingAppObjects.ro_divisionChild).DivisionId, myLeague.LeagueId)

            mydivision.leagueId = myLeague.LeagueId
            mydivision.DivisionName = Me.txtDivision.Text
            mydivision.DivisionDescription = Me.txtDivisionDesc.Text
            mydivision.Save()
        End If
    End Sub



End Class